import java.util.ArrayList;
import java.util.List;

class Solution1 {
    public static void main(String[] args) {
        String ss = "cbaebabacd";
        String pp = "abc";
        findAnagrams(ss,pp);
    }
    public static List<Integer> findAnagrams(String ss, String pp) {
        List<Integer> ret = new ArrayList<>();
        char[] s = ss.toCharArray();
        char[] p = pp.toCharArray();
        int[] hashs = new int[26]; //统计窗口中每一个字符出现的个数
        int[] hashp = new int[26]; //统计p字符串中每个字母出现的个数
        for(char ch : p) {
            hashp[ch - 'a']++;
        }
        int m = p.length;
        int count = 0; // 记录s中有效字符的个数
        for(int left = 0 ,right = 0; right < s.length; right++) {

            char in = s[right];

            if(++hashs[in - 'a'] <= hashp[in - 'a']) {//进窗口 + 维护count
                count++;
            }

            if(right - left + 1 > m) {//出窗口 + 维护count
                char out = s[left++];

                if(hashs[out - 'a']-- <= hashp[out - 'a']) {
                    count--;
                }
            }

            if(count == m) {
                ret.add(left);
            }
        }
        return ret;
    }
}
